package com.ascainiao3d.display
{
	import flash.display.Graphics;

	/**
	 * ...
	 * @author ss s
	 */
	public class Cube3D extends Object3D
	{
		protected var points:Vector.<Point3D>;
		protected var triangles:Vector.<Triangle>;
		private var unit:Number=Math.PI/180;
		public var color=0;
		public function Cube3D(n:Number,color:uint=0xffffff) 
		{
			points=new Vector.<Point3D>();
			// 前面四个角
			points[0] = new Point3D(-n/2,-n/2,-n/2);
			points[1] = new Point3D(n/2,-n/2,-n/2);
			points[2] = new Point3D(n/2,n/2,-n/2);
			points[3] = new Point3D(-n/2,n/2,-n/2);
			// 后面四个角
			points[4] = new Point3D(-n/2,-n/2,n/2);
			points[5] = new Point3D(n/2,-n/2,n/2);
			points[6] = new Point3D(n/2,n/2,n/2);
			points[7] = new Point3D(-n/2,n/2,n/2);
			for (var i:uint = 0; i < points.length; i++)
			{
				//points[i].setVanishingPoint(vpX, vpY);
				points[i].setCenter(0, 0, 0);
			}
			triangles=new Vector.<Triangle>();
			// front
			triangles[0] = new Triangle(points[0],points[1],points[2],color);//0x6666cc
			triangles[1] = new Triangle(points[2],points[3],points[0],color);
			// top
			triangles[2] = new Triangle(points[5],points[1],points[0],color);
			triangles[3] = new Triangle(points[0],points[4],points[5],color);
			//back
			triangles[4] = new Triangle(points[6],points[5],points[4],color);
			triangles[5] = new Triangle(points[4],points[7],points[6],color);
			// bottom
			triangles[6] = new Triangle(points[3],points[2],points[6],color);
			triangles[7] = new Triangle(points[6],points[7],points[3],color);
			// right
			triangles[8] = new Triangle(points[1],points[5],points[6],color);
			triangles[9] = new Triangle(points[6],points[2],points[1],color);
			// left
			triangles[10] = new Triangle(points[4],points[0],points[3],color);
			triangles[11] = new Triangle(points[3],points[7],points[4],color);
			
		}
		
		override public function draw(graphics:Graphics):void
		{
			for (var i:uint = 0; i < triangles.length; i++)
			{
				triangles[i].draw(graphics);
			}
		}
		
		override public function setVariable(light:Light,vpX:Number, vpY:Number,cX:Number,cY:Number,cZ:Number):void
		{
			// TODO Auto Generated method stub
			//super.setVariable(vpX, vpY, cX, cY, cZ);
			for (var i:uint = 0; i < points.length; i++)
			{
				points[i].setVanishingPoint(vpX, vpY);
				points[i].setCenter(cX, cY, cZ);
			}
			for (i = 0; i < triangles.length; i++)
			{
				triangles[i].light = light;
			}
		}
		
		override public function rotateX(angleX:Number):void
		{
			// TODO Auto Generated method stub
			//super.rotateX(angleX);
			for (var i:uint = 0; i < points.length; i++)
			{
				points[i].rotateX(angleX);
				//points[i].setCenter(0, 0, cZ);
			}
		}
		
		override public function rotateY(angleY:Number):void
		{
			// TODO Auto Generated method stub
			//super.rotateY(angleY);
			for (var i:uint = 0; i < points.length; i++)
			{
				points[i].rotateY(angleY);
				//points[i].setCenter(0, 0, cZ);
			}
		}
		/**
		 * 距离其原始方向的 x 轴和 y 轴的旋转（以度为单位）
		 * @param angleX
		 * @param angleY
		 * 
		 */		
		override public function rotationXY(angleX:Number, angleY:Number):void
		{
			// TODO Auto Generated method stub
			//super.rotationXY(angleX, angleY);
			for (var i:uint = 0; i < points.length; i++)
			{
				var point:Point3D = points[i];
				point.rotationXY(angleX*unit,angleY*unit);
				//point.rotateX(angleX);
				//point.rotateY(angleY);
			}
			triangles.sort(compare);
			//graphics.clear();
		}
		
		override public function snapshot():void
		{
			// TODO Auto Generated method stub
			super.snapshot();
			for (var i:uint = 0; i < points.length; i++)
			{
				var point:Point3D = points[i];
				point.snapshot();
			}
		}
		
		override public function set x(value:Number):void
		{
			// TODO Auto Generated method stub
			super.x = value;
			for (var i:uint = 0; i < points.length; i++)
			{
				points[i].x=value;
			}
		}
		
		override public function set y(value:Number):void
		{
			// TODO Auto Generated method stub
			super.y = value;
			for (var i:uint = 0; i < points.length; i++)
			{
				points[i].y=value;
			}
		}
		
		override public function set z(value:Number):void
		{
			// TODO Auto Generated method stub
			super.z = value;
			for (var i:uint = 0; i < points.length; i++)
			{
				points[i].z=value;
			}
		}
		
		override public function setPosition(x:Number, y:Number, z:Number):void
		{
			// TODO Auto Generated method stub
			//super.setPosition(x, y, z);
			var point:Point3D;
			for (var i:uint = 0; i < points.length; i++)
			{
				point= points[i];
				point.x+=x;
				point.y+=y;
				point.z+=z;
			}
		}
		
		/**
		 * 排序，按照空间深度排列
		 * @param x
		 * @param y
		 * @return 
		 * 
		 */		
		private function compare(x:Triangle, y:Triangle):Number {
			if(x.depth<y.depth){
				return 1;
			}else if(x.depth>y.depth){
				return -1;
			}else{
				return 0;
			}
		}
		
		override public function get x():Number
		{
			// TODO Auto Generated method stub
			var point:Point3D;
			var sum:Number=0;
			for (var i:uint = 0; i < points.length; i++)
			{
				point= points[i];
				sum+=point.x;
			}
			return sum/points.length;
		}
		
		override public function get y():Number
		{
			// TODO Auto Generated method stub
			var point:Point3D;
			var sum:Number=0;
			for (var i:uint = 0; i < points.length; i++)
			{
				point= points[i];
				sum+=point.y;
			}
			return sum/points.length;
		}
		
		override public function get z():Number
		{
			// TODO Auto Generated method stub
			var point:Point3D;
			var sum:Number=0;
			for (var i:uint = 0; i < points.length; i++)
			{
				point= points[i];
				sum+=point.z;
			}
			return sum/points.length;
		}
		public function set maskTop(value:Boolean):void{
			triangles[2].is_mask=value;
			triangles[3].is_mask=value;
		}
		public function set maskBottom(value:Boolean):void{
			triangles[6].is_mask=value;
			triangles[7].is_mask=value;
		}
		public function set maskRight(value:Boolean):void{
			triangles[8].is_mask=value;
			triangles[9].is_mask=value;
		}
		public function set maskLeft(value:Boolean):void{
			triangles[10].is_mask=value;
			triangles[11].is_mask=value;
		}
	}

}